package com.ruoyi.api.kehu;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.JiajiaBanner;
import com.ruoyi.system.domain.JiajiaItem;
import com.ruoyi.system.domain.JiajiaMessage;
import com.ruoyi.system.domain.JiajiaUser;
import com.ruoyi.system.service.IJiajiaBannerService;
import com.ruoyi.system.service.IJiajiaItemService;
import com.ruoyi.system.service.IJiajiaMessageService;
import com.ruoyi.system.service.IJiajiaUserService;
import com.ruoyi.system.utils.PayUtil;
import com.ruoyi.system.utils.TokenUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@RestController
@RequestMapping("/api/kehu/home")
@Api(tags = "首页")
public class ApiHomeController {

    @Autowired
    private IJiajiaItemService jiajiaItemService;

    @Autowired
    private IJiajiaUserService userService;

    @Autowired
    private IJiajiaBannerService bannerService;

    @Autowired
    private IJiajiaMessageService messageService;

    @Autowired
    private PayUtil payUtil;


    @ApiOperation("首页")
    @PostMapping("/home")
    public AjaxResult home(@RequestParam Integer sort,
                           @RequestParam(required = false) String typeCode,
                           @RequestParam(required = false) Integer lng,
                           @RequestParam(required = false) Integer lat,
                           @RequestParam Integer pageNo,
                           @RequestParam Integer pageSize,
                           @RequestParam(required = false) String jsCode)
    {

        JSONObject resObj = new JSONObject();

        JiajiaBanner banner = new JiajiaBanner();
        banner.setIsDel(0);
        PageHelper.orderBy("sort asc");
        resObj.put("bannerLists",bannerService.selectJiajiaBannerList(banner));

        //下发项目 ,需要排序 如价格，综合等
        JiajiaItem jiajiaItem = new JiajiaItem();
        String orderBy = "disPrice asc,xiaoLiang desc,haoPing desc";
        if (sort == 1){
            orderBy = "disPrice asc";
        }
        if (sort == 2){
            orderBy = "xiaoLiang desc";
        }
        if (sort == 3){
            orderBy = "haoPing desc";
        }
        PageHelper.startPage(pageNo,pageSize,orderBy);
        jiajiaItem.setIsShow(1);
        jiajiaItem.setTypeCode(typeCode);
        List<JiajiaItem> itemList =  jiajiaItemService.selectJiajiaItemList(jiajiaItem);

        resObj.put("itemList",itemList);

        resObj.put("isRead",0);//0不展示小红点 1展示

        JiajiaUser user = userService.selectJiajiaUserById(TokenUtil.getUid());

        if (!StringUtils.isEmpty(jsCode)){
            if (user != null){
                user.setOpenId(payUtil.getWxOpenIdByJsCode(jsCode));
                userService.updateJiajiaUser(user);
            }
        }

        if (user != null){
            long count = PageHelper.count(new ISelect() {
                @Override
                public void doSelect() {
                    JiajiaMessage message = new JiajiaMessage();
                    message.setUid(user.getId());
                    message.setType(2);
                    message.setIslook(0);
                    messageService.selectJiajiaMessageList(message);
                }
            });
            if (count > 0){
                resObj.put("isRead",1);//0不展示小红点 1展示
            }

        }
        System.out.println("--------------------------"+resObj+"--------------------------------------");
        return AjaxResult.success(resObj);
    }


}
